use strict;
use warnings;

package JasPerl::Compiler::JspCompilationContext;

use File::Spec::Functions;
use JasPerl::TagExt::TagLibrary;
use Module::Load;

sub new {
    my ($class, %param) = @_;
    bless {
        srcdir => $param{srcdir},
        libdir => $param{libdir},
        package => defined $param{package} ? $param{package} : 'Local::JSP',
        modext => $param{modext} || '.pm',
        compiler => $param{compiler} || 'JasPerl::Compiler::JspCompiler',
        parser => $param{parser} || 'JasPerl::Compiler::SimpleParser'
    }, $class;
}

sub get_real_path {
    my ($self, $path) = @_;
    my @pseg = grep { length } split('/', $path);
    return catfile($self->{srcdir}, @pseg);
}

sub get_page_encoding {
    return 'ISO-8859-1';
}

sub is_el_ignored {
    return 0;
}

sub get_module_name {
    my ($self, $path) = @_;
    my @pseg = map {
        s/(\W|_)+/_/g;
        length($_) ? ($_) : ()
    } split('/', $path);
    unshift @pseg, $self->{package} if $self->{package};
    return join('::', @pseg);
}

sub get_module_path {
    my ($self, $path) = @_;
    my @pseg = split('::', $self->get_module_name($path));
    return catfile($self->{libdir}, @pseg) . $self->{modext};
}

sub get_taglib {
    my ($self, $uri) = @_;
    return JasPerl::TagExt::TagLibrary->taglib($uri);
}

sub get_parser {
    my ($self, $handler) = @_;
    load $self->{parser} unless ref $self->{parser};
    return $self->{parser}->new($handler);
}

sub get_compiler {
    my $self = shift;
    load $self->{compiler} unless ref $self->{compiler};
    return $self->{compiler}->new($self);
}

1;
